package common.classes;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class SimpleServer {
	int currentCount;
	int percent;
	String to;
	
	public SimpleServer() {
		super();
		this.currentCount = 0;
		this.percent = 0;
		to = "";
	}
	
	public SimpleServer(String to, int percent) {
		super();
		this.currentCount = 0;
		this.percent = percent;
		this.to = new String(to);
	}
	
	public boolean canGet(int count) {
		if (count == 0) return true;
		return ((((double)this.currentCount)/((double)count))*100 < percent);
	}
	
	public void get(Socket socket) {
		try {
			OutputStream os = socket.getOutputStream();
			os.write("HTTP\\1.1 302 Multiple Choices\r\n".getBytes());
			os.write(("Location: " + this.to + "\r\n\r\n").getBytes());
			
			os.flush();
		} catch (FileNotFoundException e) {
			System.err.println(e);
		} catch (IOException e) {
			System.err.println(e);
		} finally {
			this.currentCount++;
			try {
				socket.close();
			} catch (IOException e) {
				System.err.println(e);
			}
		}	
	}
}
